In computing, a line number is a method used to specify a particular sequence of characters in a text file. The most common method of assigning numbers to lines is to assign every line a unique number, starting at 1 for the first line, and incrementing by 1 for each successive line.
In the C programming language the line number of a source code line is one greater than the number of new-line characters read or introduced up to that point.
Programmers could also assign line numbers to statements in older programming languages, such as Fortran, JOSS, and BASIC. In Fortran, not every statement needed a line number, and line numbers did not have to be in sequential order. The purpose of line numbers was for branching and for reference by formatting statements.
Both JOSS and BASIC made line numbers a required element of syntax. The primary reason for this is that most operating systems at the time lacked interactive text editors; since the programmer's interface was usually limited to a line editor, line numbers provided a mechanism by which specific lines in the source code could be referenced for editing, and by which the programmer could insert a new line at a specific point. Line numbers also provided a convenient means of distinguishing between code to be entered into the program and direct mode commands to be executed immediately when entered by the user (which do not have line numbers).
Largely due to the prevalence of interactive text editing in modern , line numbers are not a feature of most programming languages, even modern Fortran and Basic.
DIMENSION ALPHA(25), RHO(25)
1) FORMAT(5F12.4)
2) READ 1, ALPHA, RHO, ARG
SUM = 0.0
DO 3 I=1, 25
IF (ARG-ALPHA(I)) 4,3,3
3) SUM = SUM + ALPHA(I)
4) VALUE = 3.14159*RHO(I-1)
PRINT 1, ARG, SUM, VALUE
GO TO 2
Like assembler language before it, Fortran did not assume every line needed a label (line number, in this case). Only statements referenced elsewhere required a line number:
While the line numbers are sequential in this example, in the very first "complete but simple Fortran program" published the line numbers are in the sequence 1, 5, 30, 10, 20, 2. (2+51+1 pages)
Line numbers could also be assigned to fixed-point variables (e.g., i n) for referencing in subsequent assigned GO TO statements (e.g., n,(n1,n2,...nm)).
Unlike FORTRAN before it or BASIC after it, JOSS required line numbers to be fixed-point numbers consisting of a pair of two-digit integers separated by a period (e.g., 1.1). The portion of the line number to the left of the period is known as the "page" or "part", while the portion to the right is known as the "line"; for example, the line number refers to page 10, line 12. Branches can target either a page or a line within a page. When the later format is used, the combined page and line is known as a "step".
Pages are used to define , which return when the next line is on a different page. For instance, if a subroutine for calculating the square root of a number is in page 3, one might have three lines of code 3.1, 3.2 and 3.3, and it would be called using The code would return to the statement after the Do when it reaches the next line on a different page, for instance, 4.1. There is no need for the equivalent of a at the end, although if an early return is required, accomplishes this. Example:
*Routine to ask the user for a positive value and repeat until it gets one 01.10 Demand X as "Enter a positive value greater than zero". 01.20 Done if X>0. 01.30 To step 1.1
Line numbers were rarely used elsewhere. One exception was allowing the pointer used by (which iterated through statements) to be set to a specific line number using .
1 REM RESTORE COULD BE USED IF A BASIC LACKED STRING ARRAYS
2 DIM M$(9): REM DEFINE LENGTH OF 9 CHARACTERS
5 INPUT "MONTH #?"; M: IF M<1 OR M>12 THEN 5
7 RESTORE 10*M: READ M$: PRINT M$
10 DATA "JANUARY"
20 DATA "FEBRUARY"
30 DATA "MARCH"
...
In the first editions of Dartmouth BASIC, could only be followed by a line number (for an implied GOTO), not - as in later implementations - by a statement.
The range of valid line numbers varied widely from implementation to implementation, depending on the representation used to store the binary equivalent of the line number (one or two bytes; signed or unsigned). While Dartmouth BASIC supported 1 to 99999, the typical microcomputer implementation supported 1 to 32767 (a signed 16-bit word).
+ Valid Line Numbers in Early BASIC Implementations |
MINOL |
Tiny BASIC Design Note |
Denver Tiny BASIC |
UIUC BASIC |
BASIC-PLUS |
LLL BASIC, NIBL |
Integer BASIC, Level I BASIC, Palo Alto Tiny BASIC |
GW-BASIC, IBM BASIC |
Altair BASIC, MICRO BASIC 1.3, 6800 Tiny BASIC, Tiny BASIC Extended |
Dartmouth BASIC |
SCELBAL |
QBASIC 1) |
Some BASICs had a command, which typically would go through the program (or a specified portion of it), reassigning line numbers in equal increments. It would also renumber all references to those line numbers so they would continue to work properly.
In a large program containing , each subroutine would usually start at a line number sufficiently large to leave room for expansion of the main program (and previous subroutines). For example, subroutines might begin at lines 10000, 20000, 30000, etc.
GOTO-style branching can lead to the development of spaghetti code. (See Considered harmful, Structured programming.) Even in some later versions of BASIC that still mandated line numbers, the use of line number-controlled GOTOs was phased out whenever possible in favor of cleaner constructs such as the for loop and while loop.
Many modern languages (including C and C++) include a version of the GOTO statement; however, in these languages the target of a GOTO is specified by a line label instead of a line number.
The use of line numbers to describe the location of errors remains standard in modern programming tools, even though line numbers are never required to be manually specified. It is a simple matter for a program to count the in a source file and display an automatically generated line number as the location of the error. In IDEs such as Microsoft Visual Studio, Eclipse or Xcode, in which the compiler is usually integrated with the text editor, the programmer can even double-click on an error and be taken directly to the line containing that error.
|
|